<template>
	<view class="bg"
		:style="{background:`center center/cover no-repeat`,backgroundImage:`url(${imgPath}${placeImg}_${num}.jpg)`}"
		@error='handleImageError'>
		<HeaderTop>
			<template v-slot:title>
				<view>福利</view>
			</template>
		</HeaderTop>
		<view class="happyAcceptanceBorder mediaBox">
			<button class="happyAcceptanceBtn" @click="receiveFn">开心收下</button>
		</view>
	</view>
</template>
<script>
	import placeMap from '@/libs/place-map.js';
	export default {
		data() {
			return {
				imgPath: '', // 图片路径
				img: '',
				place: {},
				num: 0, // 次数
				placeImg: '', // 地区编码
				imageUrl: '',
			};
		},
		onLoad(payload) {
			console.log(payload)
			// http://images.linglinggong.net/static/BD_tg/330783_1.jpg
			this.num = payload.scanCount;
		},
		created() {
			let regionObj = uni.getStorageSync('region1');
			this.place = placeMap();
			// 地区判断如果有的话 就是执行对应地区的
			if (regionObj) {
				let region = regionObj.slice(0, 6)
				console.log('有区域', this.place[region]); // 东阳市 - 其他区/市
				// 地区编码
				this.placeImg = uni.getStorageSync('region1').slice(0, 6);
				this.imageUrl = this.imgPath + this.placeImg + '_' + this.num;
			} else {
				// 没有的话默认就是 义乌市 
				// 没有话，默认是 330782
				uni.setStorageSync('region1', '330782001');
				console.log('没有区域,设置默认', this.place[330782]); // 义乌市
				this.placeImg = '330782'; // 默认就是固定的
				this.num = 2;
			}
			let tempImgPath = uni.getStorageSync('imgPath');
			// 做验证 以防没有获取到本地存储的值
			// 如果存在的话就正常给 imgPath 赋值
			if (tempImgPath) {
				this.imgPath = tempImgPath + 'static/BD_tg/';
			} else {
				// 如果不存在的话依旧给死值
				this.imgPath = 'http://images.linglinggong.net/static/BD_tg/';
			}
		},
		methods: {
			async receiveFn() {
				// 点击完 收下 就跳转到 首页 把状态调成 no 下次刷新就不会进到活动页
				uni.setStorageSync('tempState', 'no');
				// // /nt/user/executeBind
				let res = await this.$fetch('/nt/user/executeBind', {}, "POST");
				console.log(res);
				if (res.code != 0) {
					uni.showToast({
						title: res.msg,
						icon: "none",
						duration: 1500
					})
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 2000)
				} else {
					uni.switchTab({
						url: "/pages/index/index"
					})
					console.log('打印返回');
				}
			}
		},
		onUnload() {
			uni.setStorageSync('tempState', 'no');
			uni.switchTab({
				url: "/pages/index/index"
			})
		}
	}
</script>
<style lang="less" scoped>
	.bg {
		width: 100vw;
		height: 1624rpx;
		position: absolute;
		top: 0;
		// background: url('http://images.linglinggong.net/static/BD_tg/lgz_330783_BD.jpg')center center/cover no-repeat;
	}

	.happyAcceptanceBorder {
		width: 75%;
		position: absolute;
		background: rgb(255, 225, 161);
		border-radius: 80rpx;
		padding: 15rpx;
		box-shadow: 0rpx 5rpx 20rpx rgba(255, 0, 0, .3);
		bottom: 288rpx;
		left: 50%;
		transform: translateX(-50%);

		.happyAcceptanceBtn {
			width: 100%;
			height: 100%;
			border-radius: 80rpx;
			background: rgb(254, 76, 40);
			color: white;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>